package com.ruoyi.collector.service;

import com.ruoyi.system.domain.GaeaReportDashboardWidget;
import io.krakens.grok.api.Grok;

import io.krakens.grok.api.GrokCompiler;
import io.krakens.grok.api.Match;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @CreateTime: 2023-01-01  17:01
 * @Author: chaozi
 * @Version: 1.0
 */
@Service
public class MessageParser {
    private static final Logger log = LoggerFactory.getLogger(MessageParser.class);

    public Map parse(String name,String message) throws Exception {
        try {
            GrokCompiler grokCompiler = GrokCompiler.newInstance();
            grokCompiler.registerDefaultPatterns();
            List<String> patterns = new LinkedList<>();
            String patternString = String.join("|", patterns);
            final Grok grok = grokCompiler.compile("%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:host} %{DATA:process}: %{GREEDYDATA:message}");
//            Grok grok = grokCompiler.compile(patternString);
            String log = "Jan  7 00:01:29 seclog01 sshd[4962]: Failed password for 张三 5156 from 5.8.66.78 port 50790 ssh2";
//            Match gm = grok.match(message);
            Match gm = grok.match(log);
            /* Get the map with matches */
            final Map<String, Object> capture = gm.capture();
            System.out.println(capture.toString());
            return capture;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }


    }

    public static void main(String[] args) {
        try {
            GrokCompiler grokCompiler = GrokCompiler.newInstance();
            grokCompiler.registerDefaultPatterns();
            List<String> patterns = new LinkedList<>();
            String patternString = String.join("|", patterns);
            final Grok grok = grokCompiler.compile("%{SYSLOGTIMESTAMP:timestamp} %{SYSLOGHOST:host} %{DATA:process}: %{GREEDYDATA:message}");
//            Grok grok = grokCompiler.compile(patternString);
            String log = "Jan  7 00:01:29 seclog01 sshd[4962]: Failed password for 张三 5156 from 5.8.66.78 port 50790 ssh2";
//            Match gm = grok.match(message);
            Match gm = grok.match(log);
            /* Get the map with matches */
            final Map<String, Object> capture = gm.capture();
            System.out.println(capture.toString());
        }catch (Exception e){
            e.printStackTrace();

        }
    }
    public Map testParse(String patternString,String message) {
        try {
            GrokCompiler grokCompiler = GrokCompiler.newInstance();
            grokCompiler.registerDefaultPatterns();
            Grok grok = grokCompiler.compile(patternString);
            Match gm = grok.match(message);
            final Map<String, Object> capture = gm.capture();
            System.out.println(capture.toString());
            return capture;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



}
